package bjtu.cit.bookshop.controller.admin;

import bjtu.cit.bookshop.common.ErrorCode;
import bjtu.cit.bookshop.controller.BaseController;
import bjtu.cit.bookshop.entities.Book;
import bjtu.cit.bookshop.entities.Note;
import bjtu.cit.bookshop.entities.NoteWithBLOBs;
import bjtu.cit.bookshop.entities.User;
import bjtu.cit.bookshop.service.INoteService;
import bjtu.cit.bookshop.service.IUserService;
import org.omg.CORBA.BAD_PARAM;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jia19 on 2015/10/19.
 */
@Controller
@RequestMapping("/admin")
public class NoteController extends BaseController {

    @Resource
    INoteService noteService = null;
    @Resource
    IUserService userService = null;

    @RequestMapping("note.do")
    public String note(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }

        String page = request.getParameter("page");

        if (page == null) {
            page = "1";
        }
        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "note");



        List<NoteWithBLOBs> noteList = noteService.getNoteList(10, (Integer.valueOf(page) - 1) * 10);
        model.addAttribute("noteList", noteList);
        if (noteList == null) {
            noteList = new ArrayList<NoteWithBLOBs>();
        }
        int len = noteService.getNoteLength();
        model.addAttribute("totalPage", len / 10 + (len % 10 == 0?0:1));
        model.addAttribute("thisPage", page);


        return "/admin/note";

    }

    @RequestMapping("noteReply.do")
    public String noteReply(HttpSession session, HttpServletRequest request, ModelMap model) {
        String loginName = checkAdminLogin(session);
        if (loginName == null) {
            return "/admin/index";
        }

        model.addAttribute("isLogin", true);
        model.addAttribute("loginName", loginName);
        model.addAttribute("current", "note");

        String id = request.getParameter("id");
        if (id == null) {
            return "admin/badParameter";
        }

        try {
            NoteWithBLOBs note = noteService.getNoteById(Integer.valueOf(id));
            if (note == null) {
                return "admin/badParameter";
            }
            model.addAttribute("note", note);

            User user = userService.getUserById(note.getUserid());
            model.addAttribute("username", user.getUsername());

        } catch (Exception e) {
            e.printStackTrace();
            return "admin/badParameter";
        }
        return "admin/noteReply";
    }

    @RequestMapping("processNote.do")
    @ResponseBody
    public ErrorCode processNote(HttpSession session, HttpServletRequest request) {
        if (checkAdminLogin(session) == null) {
            return NO_AUTH;
        }

        String reply = request.getParameter("reply");
        String id = request.getParameter("id");

        try {
            NoteWithBLOBs note = noteService.getNoteById(Integer.valueOf(id));
            note.setDate2(new Date());
            note.setEmployeeid((Integer)session.getAttribute("userId"));
            note.setReply(reply);
            noteService.updateNoteInfoById(note);
        } catch (Exception e) {
            e.printStackTrace();
            return PARA_ERROR;
        }

        return SUCCESS;
    }
}
